package com.apes.scm.stock.transfer.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.account.model.Stock;
import com.apes.scm.account.account.repository.StockRepository;
import com.apes.scm.account.account.service.ProductAccountService;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 功能： 转储订单（公司内）
 *
 * @author wuq
 * @create 2018-06-21 17:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TransferItem.CONST_TABLE_NAME)
public class TransferItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "stock_transfer_item";

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "transfer_id", referencedColumnName = "id")
    private Transfer transfer;

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "转储订单明细行")
    @Comment("订单明细行编码")
    private long id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品编码")
    private Product product; //商品

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位编码")
    private ProductUom uom; //计量单位

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "supplier_id", referencedColumnName = "id", nullable = false)
//    private PartyRole supplier; //供应商编码
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "purchasing_organization_id", referencedColumnName = "id", nullable = false)
//    private PartyRole organization; //采购组织编码

    @Column(name = "transfer_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("转储数量")
    private double transferQty = 0; //转储数量

    @Column(name = "qty_received", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("已收货数量")
    private double qtyReceived = 0; //已收货数量

    @Column(name = "send_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已发货数量")
    private double sendQty = 0; //已发货数量

    @Column(name = "finish_sum", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("终止数量")
    private double finishSum = 0; //终止数量

    @Column(name = "note", length = 120)
    @Comment("备注")
    private String note; //备注

    @Column(name = "origin_item_id")
    @Comment("原始明细编码")
    private String originItemId;

    @Transient
    private double usableQty;   // 可用数

    public double getUsableQty() {
        // 审核过的单据不查询
        if (!"approve".equals(this.transfer.getState())) {

            Map m = SpringManager.getBean(ProductAccountService.class).getUomUsableQty(
                   transfer.getOriginLocation(), product, uom,  transfer.getOriginLocation().getCompany()
            );
            if (m.isEmpty()) return 0.00;

            return (Double) m.get("marketableQty") + transferQty;

        }
        return 0;
    }

    public boolean calcQty(){
        return transferQty == sendQty && transferQty == qtyReceived;
    }

    @Transient
    private double factor;

    public double getFactor() {
        if (uom != null) return 1/uom.getFactor();
        return 0;
    }

    @Transient
    private double withDrawDiveryQty = 0;

    public double getWithDrawDiveryQty() {
        return this.withDrawDiveryQty;
    }

}
